<!-- 关联人员 -->
<template>
	<view class="">
		<u-sticky offset-top="0">
			<common-search class="search" v-model="materialName" :isScreen="true" @screenclick="searchMsg"
				@searchMsg="searchMsg" placeholder="请输入物料名称"></common-search>
		</u-sticky>
		<view class="page"
			:style="!isData?'display:flex;width: 100vw;height:100vh;align-items:center;justify-content: center;':''">
			<template v-if="isData">
				<view class="list" v-for="(item,index) in dataList" :key="index">
					<view class="">
						<view @click="onChecker(item, index)">
							<view class="option">
								<view class="lf">物料名称</view>
								<view class="rl">{{ item.materialName }}</view>
							</view>
							<view class="option">
								<view class="lf">物料编码</view>
								<view class="rl">{{ item.materialBill }}</view>
							</view>
							<view class="option">
								<view class="lf">规格型号</view>
								<view class="rl">{{ item.specification }}</view>
							</view>
							<view class="option">
								<view class="lf">单位</view>
								<view class="rl">{{ item.unit_dictText }}</view>
							</view>
							<view class="option">
								<view class="lf">品牌</view>
								<view class="rl">{{ item.brand }}</view>
							</view>
							<view class="option">
								<view class="lf">物料类别</view>
								<view class="rl">{{ item.provisionBroad_dictText }}-{{ item.provisionSmall_dictText }}
								</view>
							</view>
						</view>
					</view>
					<view class="icon" v-show="item.isChecker"><u-icon name="checkbox-mark" color="#2979ff"
							size="22"></u-icon></view>
				</view>
				<!-- 加载更多显示 -->
				<view class=""><u-loadmore :status="statusLoadmore" /></view>
			</template>
			<view v-else>
				<u-empty mode="data" icon="/static/empty/data.png">
				</u-empty>
			</view>
			<view style="height: 100rpx;"></view>
			<view class="btn">
				<u-button text="确定" type="primary" @click="onConfirm"></u-button>
			</view>
		</view>

		<u-toast ref="uToast"></u-toast>
	</view>
</template>
<script>
	import {
		erpPurchaseMaterialList
	} from '@/api/index.js'
	import CommonSearch from '@/components/CommonSearch.vue'
	export default {
		components: {
			CommonSearch
		},
		data() {
			return {
				selectedItem: {}, //选中的项
				materialName: '',
				provisionSmall: '',
				showNotify: true, // 消息提示
				id: '', // 选中的id
				page: {
					pageNo: 1,
					pageSize: 10,
				},
				dataList: [], // 列表
				statusLoadmore: 'loadmore', // 通过status设置组件的状态，加载前值为loadmore，加载中为loading，没有数据为nomore
				oldIndex: 0, // 存储列表之前被选中的数据
				objectField: '', // 点击确认需要修改的字段
				eventChannel: null
			}
		},
		computed: {
			isData() {
				return !!this.dataList.length
			}
		},
		onLoad(options) {
			this.eventChannel = this.getOpenerEventChannel();
			this.provisionSmall = options.provisionSmall
			if (this.provisionSmall) {
				this.getList()
			}
		},
		methods: {
			// 获取列表
			getList(flag) {
				this.statusLoadmore = 'loading' // 加载中
				erpPurchaseMaterialList({
					...this.page,
					auditStatus: 2,
					materialName: this.materialName
				}).then(res => {
					if (res.success && res.result?.records?.length) {
						const {
							records
						} = res.result
						const dataList = records.map(i => {
							i.isChecker = this.applyId == i.id
							return i
						})
						if (flag) {
							this.dataList = dataList
						} else {
							this.dataList = [...this.dataList, ...dataList]
						}
						this.statusLoadmore = 'nomore'
					}
				})
			},
			// 点击选中取消选中
			onChecker(item, index) {
				this.dataList.forEach(i => i.isChecker = false)
				item.isChecker = !item.isChecker
				this.selectedItem = item
			},
			searchMsg() {
				this.page.pageNo = 1
				this.getList(true)
			},
			onReachBottom() {
				this.page.pageNo++
				this.getList()
			},
			// 点击确定
			onConfirm() {
				if (!this.selectedItem.id) {
					this.$refs.uToast.show({
						type: 'error',
						icon: 'none',
						message: '请选择数据'
					})
					return
				}
				this.eventChannel.emit('pageEvent',this.selectedItem)
				// let pages = getCurrentPages()
				// let prevPage = pages[pages.length - 2] //上一页页面实例
				// prevPage.$vm.$refs.dataForm.basicForm.id = this.selectedItem.id
				// prevPage.$vm.$refs.dataForm.basicForm.materialName = this.selectedItem.materialName
				// prevPage.$vm.$refs.dataForm.basicForm.unit_dictText = this.selectedItem.unit_dictText
				// prevPage.$vm.$refs.dataForm.basicForm.materialBill = this.selectedItem.materialBill
				// prevPage.$vm.$refs.dataForm.basicForm.specification = this.selectedItem.specification
				// prevPage.$vm.$refs.dataForm.basicForm.brand = this.selectedItem.brand
				uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1
				});
			}


		},
	}
</script>
<style lang="scss" scoped>
	.page {
		padding: 0 20rpx 20rpx 20rpx;

		.list {
			border-radius: 10rpx;
			box-shadow: 0px 4px 20px 0px rgba(0, 0, 0, 0.1);
			padding: 12rpx 20rpx 20rpx 20rpx;
			background: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 20rpx;
		}
	}

	// 按钮
	.btn {
		position: fixed;
		bottom: 20rpx;
		width: 95%;
	}
</style>